<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="物料代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCode">
              <a-select
                :dropdownMatchSelectWidth="false"
                :showSearch="true"
                style="width: 100%"
                :value="selectValue"
                @focus="searchMaterial"
                @select="changeRecord($event)"
                :filter-option="false"
              >
                <a-select-option v-for="(item, index) in materialData" :key="index">
                  <span v-for="(i, index) in materialField" :key="index">
                    {{ item[i] }}
                    <a-divider type="vertical" style="background-color: #0c8fcf"></a-divider>
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.materialName" placeholder="请输入物料名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="false">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" placeholder="请输入单位" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit_dictText">
              <a-input v-model="model.unit_dictText" placeholder="请输入单位" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="物料类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="materialType_dictText"
            >
              <a-input v-model="model.materialType_dictText" placeholder="请输入物料类型" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="false">
            <a-form-model-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <a-input v-model="model.materialType" placeholder="请输入物料类型" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input-number v-model="model.number" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" placeholder="请输入单价" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tax">
              <a-input-number v-model="model.tax" placeholder="请输入税率" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="含税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxPrice">
              <a-input-number v-model="model.taxPrice" placeholder="请输入含税单价" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="价税合计" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalPrice">
              <a-input-number v-model="model.totalPrice" placeholder="请输入价税合计" style="width: 100%" disabled />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'HfPurchaseRequestDetailModal',
  components: {},
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    },
    supplierId: {
      type: String,
      required: false,
      default: ''
    },
    serial: {
      type: String,
      required: false,
      default: ''
    }
  },
  watch: {
    'model.number': function(val) {
      console.log(val)
      this.model.totalPrice = val * this.model.taxPrice
    }
  },
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      selectValue: '',
      materialData: [],
      /**
       * 需要配合下拉选择修改的字段
       */
      materialField: ['productCode', 'productName', 'sap', 'unit', 'materialType'],

      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/purchase/hfPurchaseRequest/addHfPurchaseRequestDetail',
        edit: '/purchase/hfPurchaseRequest/editHfPurchaseRequestDetail',
        materialList: '/purchase/hfPurchaseprice/getUsePriceBySupplier'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
      this.selectValue = ''
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['sourceId'] = this.mainId
          this.model['sourceSerial'] = this.serial
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },

    searchMaterial() {
      let params = {
        supplierId: this.supplierId
      }
      getAction(this.url.materialList, params).then(res => {
        this.materialData = res.result.records
      })
    },
    changeRecord(index) {
      //获取物料信息
      // console.log(index)
      // console.log(this.materialData[index].materialName)
      this.model.materialCode = this.materialData[index].productCode
      this.model.materialName = this.materialData[index].productName
      this.model.sap = this.materialData[index].sap
      this.model.sap_dictText = this.materialData[index].sap_dictText
      this.model.unit = this.materialData[index].unit
      this.model.unit_dictText = this.materialData[index].unit_dictText
      this.model.materialType = this.materialData[index].materialType
      this.model.materialType_dictText = this.materialData[index].materialType_dictText
      this.model.price = this.materialData[index].price
      this.model.tax = this.materialData[index].tax
      this.model.taxPrice = this.materialData[index].taxPrice

      this.selectValue = this.materialData[index].productCode
    }
  }
}
</script>
